package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.union.cross.CrossUnionChamberPatrons;
import com.yanqu.road.logic.pb.CrossUnionPb;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_UNION_CHAMBER_DISPATCH, desc = "商会议事厅派遣")
public class CrossUnionChamberDispatchCmd extends CrossCommand {

    long playerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        CrossUnionProto.UnionChamberDispatchCrossReqMsg reqMsg = CrossUnionProto.UnionChamberDispatchCrossReqMsg.parseFrom(packet.getBytes());
        CrossUnionChamberPatrons chamberPatrons = null;
        if(reqMsg.hasPatrons()) {
            chamberPatrons = CrossUnionPb.parseCrossUnionChamberPatrons(packet.getPlayerId(), reqMsg.getPatrons());
        }
        ret = CrossUnionMgr.chamberDispatch(serverId, playerId, chamberPatrons, reqMsg.getNeedBack(), reqMsg.getPatronsId());

    }

    @Override
    public void sendData() {
        if(ret != 0) {
            UnionProto.UnionChamberDispatchRespMsg.Builder respMsg = UnionProto.UnionChamberDispatchRespMsg.newBuilder();
            respMsg.setRet(ret);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_UNION_CHAMBER_DISPATCH, respMsg));
        }
    }
}
